/*
 * HelpWindow.java This class creates a help window
 * @author Shaun Takeuchi
 * Note: html/image files must be in source path for creating jar file
 *       html/image files must also be in bin folder to run in eclipse
 */
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.net.URL;
import java.io.IOException;

public class HelpWindow extends JFrame {
	
    // create window and fill with scroll pane
    public HelpWindow() {
        JFrame frame = new JFrame("Help Menu");
        frame.setLocation(50,20); // need a better start location
        frame.setSize(800, 600);
        frame.setResizable(false);  // not sure if the window should be resizable
        frame.setLayout(new BorderLayout());
        JScrollPane editorScrollPane = createEditorPane();
        editorScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        frame.add(editorScrollPane);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    // create and fill scroll pane with editor pane
    private JScrollPane createEditorPane() {
       JEditorPane editorPane = new JEditorPane();
       editorPane.setEditorKit(JEditorPane.createEditorKitForContentType("text/html"));
       editorPane.setEditable(false);
       editorPane.addHyperlinkListener(new HelpHyperlinkListener());
       URL helpURL = HelpWindow.class.getResource("HelpHtml/index.html");
       if (helpURL != null) {
          try {
              editorPane.setPage(helpURL);
              JScrollPane scrollPane = new JScrollPane(editorPane);
              return scrollPane;
          } 
          catch (IOException e) {
             // this should not occur
             System.err.println("Attempted to read a bad URL: " + helpURL);
          }
       }
       return null; // this should not happen
    }
    
    // used to detect clicked links
    class HelpHyperlinkListener implements HyperlinkListener {  
       public void hyperlinkUpdate(HyperlinkEvent evt) {  
          if (evt.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {  
             // if link is clicked
             JEditorPane helpPane = (JEditorPane)evt.getSource();
             URL helpURL = HelpWindow.class.getResource(evt.getDescription());
             try {  
                // Show the new page in the editor pane.
                helpPane.setPage(helpURL);
             } 
             catch (IOException e) {
                // this should not occur
                System.err.println("Couldn't find file: " + evt.getDescription());  
             }  
          }  
       }  
    }
}
